const Sequelize = require('sequelize');
const { NewsModel } = require('../models/news');

const Op = Sequelize.Op;

const newsService = {

  // 通过页码，条数，类型进行列表筛选查询
  async getNewsList(page, pageSize, type) {

    const currentPage =  parseInt(page);
    const size = parseInt(pageSize);
    if (type === '') {
      return NewsModel.findAll({
        offset: size * (currentPage - 1),
        limit: size,
      })
    }
    else {
      return NewsModel.findAll({
        offset: size * (currentPage - 1),
        limit: size,
        where: {
          news_type: {
            [Op.like]: `%${type}%`
          }
        }
      })
    }
  },

  // 通过新闻id，删除数据
  async deleteNews(news_id) {
    NewsModel.destroy({
      where: {
        "news_id": news_id
      }
    })
  }
};

module.exports = newsService;